import time
import os
import sys
from colorama import init, Fore, Back, Style

# 初始化colorama，用于Windows系统兼容彩色文本
init(autoreset=True)

def typewriter_effect(text, delay=0.05, color_sequence=None):
    """
    模拟打字机效果打印文本，并可选择颜色序列
    """
    if color_sequence is None:
        color_sequence = [Fore.WHITE]  # 默认白色

    for i, char in enumerate(text):
        # 从颜色序列中循环选取颜色
        color = color_sequence[i % len(color_sequence)]
        print(color + char, end='', flush=True)  # 逐字打印
        time.sleep(delay)
    print()  # 换行

def scrolling_hello_world():
    message = "两年翻身人上人❤❤❤百万年薪不是梦❤❤❤我爱北京雁栖湖应用数学研究院❤❤❤"
    colors = [Fore.RED, Fore.YELLOW, Fore.GREEN, Fore.CYAN, Fore.BLUE, Fore.MAGENTA]
    
    try:
        while True:
            # 清屏（跨平台）
            os.system('cls' if os.name == 'nt' else 'clear')
            
            # 创建彩虹色渐变效果
            for i, char in enumerate(message):
                color = colors[(i) % len(colors)]
                print(color + char, end='')
            print()
            
            # 更新消息以实现滚动效果
            message = message[1:] + message[0]
            time.sleep(0.2)
            
    except KeyboardInterrupt:
        print(f"\n{Fore.GREEN}Animation stopped. Have a great day!")

def main():
    """主函数"""
    # 定义颜色序列（彩虹色）
    rainbow_colors = [Fore.RED, Fore.YELLOW, Fore.GREEN, 
                     Fore.CYAN, Fore.BLUE, Fore.MAGENTA]
    
    print(f"{Fore.WHITE}{Back.BLUE}=== 炫酷的Hello World演示 ==={Style.RESET_ALL}")
    print()
    
    # 第一阶段：打字机效果
    typewriter_effect("系统初始化中...", 0.03, [Fore.GREEN])
    time.sleep(0.5)
    
    typewriter_effect("正在加载炫酷特效模块...", 0.02, [Fore.CYAN])
    time.sleep(0.5)
    
    # 第二阶段：彩色Hello World
    hello_text = "Hello, World!你好，世界！✨"
    typewriter_effect(hello_text, 0.05, rainbow_colors)
    time.sleep(1)
    
    # 第三阶段：滚动动画
    print(f"\n{Fore.YELLOW}按 Ctrl+C 停止动画...{Style.RESET_ALL}")
    time.sleep(1)
    scrolling_hello_world()

if __name__ == "__main__":
    main()